package donnees;

import java.util.ArrayList;

import exceptions.VariableException;

public class FonctionObj {

	private boolean max;
	private ArrayList<Variable> vars;
	private ArrayList<Double> coeffs;
	
	public FonctionObj(boolean max, ArrayList<Variable> vars, ArrayList<Double> coeffs) throws VariableException {
		if(coeffs.size() != vars.size()) {
			throw new VariableException(0, "FonctionObj.FonctionObj()");
		}
		// on parcourt la liste des variables pour voir s'il n'y a pas 2 fois la meme
		for(int i = 0; i < vars.size(); ++i) {
			for(int j = 0; j < vars.size(); ++j) {
				if(i != j && vars.get(i).getNom().equals(vars.get(j).getNom())) {
					throw new VariableException(1,"FonctionObj.FonctionObj()");
				}
			}
		}
		this.max = max;
		this.vars = vars;
		this.coeffs = coeffs;
	}
	
	public void addVar(Variable var, Double coeff) throws VariableException {
		// on parcourt la liste des variables pour voir s'il n'y a pas 2 fois la meme
		for(int i = 0; i < vars.size(); ++i) {
			if(vars.get(i).getNom().equals(var.getNom())) {
				throw new VariableException(1,"FonctionObj.addVar()");
			}
		}
		vars.add(var);
		coeffs.add(coeff);
	}
	
	public void setCoeff(Variable var, Double coeff) throws VariableException {
		int pos = vars.indexOf(var);
		if(pos == -1) {
			throw new VariableException(2,"FonctionObj.setCoeff()");
		}
		else {
			coeffs.set(pos, coeff);
		}
	}
	
	public boolean includeVar(Variable var) {
		int i = 0;
		while(i < vars.size() && !vars.get(i).getNom().equals(var.getNom())) {
			++i;
		}
		if(i == vars.size()) {
			return false;
		}
		else {
			return true;
		}
	}

	public void removeVar(Variable var) throws VariableException {
		int pos = vars.indexOf(var);
		if(pos == -1) {
			throw new VariableException(2,"FonctionObj.removeVar()");
		}
		else {
			vars.remove(pos);
			coeffs.remove(pos);
		}
	}
	
	public boolean isMaximize() {
		return max;
	}

	public void setMaximize(boolean max) {
		this.max = max;
	}
	
	public void setFonctionObj(boolean max, ArrayList<Variable> vars, ArrayList<Double> coeffs) throws VariableException {
		if(coeffs.size() != vars.size()) {
			throw new VariableException(0, "FonctionObj.setFonctionObj()");
		}
		// on parcourt la liste des variables pour voir s'il n'y a pas 2 fois la meme
		for(int i = 0; i < vars.size(); ++i) {
			for(int j = 0; j < vars.size(); ++j) {
				if(i != j && vars.get(i).getNom().equals(vars.get(j).getNom())) {
					throw new VariableException(1,"FonctionObj.setFonctionObj()");
				}
			}
		}
		this.max = max;
		this.vars = vars;
		this.coeffs = coeffs;
	}
	
	public ArrayList<Variable> getVariables() {
		return vars;
	}
	
	public ArrayList<Double> getCoeffs() {
		return coeffs;
	}
	
	public String toString() {
		String res;
		if(max)
			res = "Max";
		else
			res = "Min";
		for(int i=0; i<vars.size() && i<coeffs.size(); ++i) {
			if(i != 0)
				res+= " +";
			res += " "+coeffs.get(i)+"*"+vars.get(i).getNom();
		}
		return res;
	}
}
